﻿using Microsoft.SqlServer.Server;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyFileView.Core.DocumentFormat
{
    public interface IDocumentFormatRegistry : RegisterAutofac
    {
        /// <summary>
        /// 获取json里的document格式数据
        /// </summary>
       // public  List<DocumentFormatData> DocumentFormat { get; set; }
        DocumentFormat GetFormatByFileExtension(string extension);

        DocumentFormat GetFormatByMimeType(string extension);


        StorePropertiesDetail GetFormatByExtension(string extension);
 
    }
    public abstract class BasicDocumentFormatRegistry:IDocumentFormatRegistry
    {
        public virtual List<DocumentFormatData> DocumentFormat { get; set; }
        private readonly IList _documentFormats = new ArrayList(); //<DocumentFormat>

        public virtual void AddDocumentFormat(DocumentFormat documentFormat)
        {
            _documentFormats.Add(documentFormat);
        }

        protected internal virtual IList DocumentFormats
        {
            get
            {
                return _documentFormats;
            }
        }

        /// <param name="extension"> the file extension </param>
        /// <returns> the DocumentFormat for this extension, or null if the extension is not mapped </returns>
        public virtual DocumentFormat GetFormatByFileExtension(string extension)
        {
            if (extension == null)
            {
                return null;
            }
            string lowerExtension = extension.ToLower();
            for (IEnumerator it = _documentFormats.GetEnumerator(); it.MoveNext();)
            {
                DocumentFormat format = (DocumentFormat)it.Current;
                if (format != null && format.FileExtension.Equals(lowerExtension))
                {
                    return format;
                }
            }
            return null;
        }
        /// <summary>
        /// 根据拓展后缀查询对应后缀数据
        /// </summary>
        /// <param name="extension"></param>
        /// <returns></returns>
        public virtual StorePropertiesDetail GetFormatByExtension(string extension)
        {
            if (string.IsNullOrEmpty(extension))
            {
                return null;
            }
            string lowerExtension = extension.ToLower();
            for (IEnumerator it = DocumentFormat.GetEnumerator(); it.MoveNext();)
            {
                DocumentFormatData format = (DocumentFormatData)it.Current;
              
                if (format != null && format.extensions.Contains(lowerExtension))
                {
                    return format.storeProperties;
                }
            }
            return null;
        }
        public virtual DocumentFormat GetFormatByMimeType(string mimeType)
        {
            for (IEnumerator it = _documentFormats.GetEnumerator(); it.MoveNext();)
            {
                DocumentFormat format = (DocumentFormat)it.Current;
                if (format != null && format.MimeType.Equals(mimeType))
                {
                    return format;
                }
            }
            return null;
        }
    }
}
